package com.hosa.Onlinemall.product.service.impl;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hosa.Onlinemall.product.vo.CategoryVo;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hosa.Onlinemall.product.mapper.CategoryMapper;
import com.hosa.Onlinemall.product.domain.Category;
import com.hosa.Onlinemall.product.service.ICategoryService;

/**
 * 商品三级分类Service业务层处理
 * 
 * @author hosa
 * @date 2025-02-15
 */
@Service
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;


    /**
     * 查询商品三级分类
     *
     * @param catId 商品三级分类主键
     * @return 商品三级分类
     */
    @Override
    public Category selectCategoryByCatId(Long catId) {
        return categoryMapper.selectCategoryByCatId(catId);
    }

    /**
     * 查询商品三级分类列表
     *
     * @param category 商品三级分类
     * @return 商品三级分类
     */
    @Override
    public List<Category> selectCategoryList(Category category) {
        return categoryMapper.selectCategoryList(category);
    }

    /**
     * 新增商品三级分类
     *
     * @param category 商品三级分类
     * @return 结果
     */
    @Override
    public int insertCategory(Category category) {
        return categoryMapper.insertCategory(category);
    }

    /**
     * 修改商品三级分类
     *
     * @param category 商品三级分类
     * @return 结果
     */
    @Override
    public int updateCategory(Category category) {
        return categoryMapper.updateCategory(category);
    }

    /**
     * 批量删除商品三级分类
     *
     * @param catIds 需要删除的商品三级分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryByCatIds(Long[] catIds) {
        return categoryMapper.deleteCategoryByCatIds(catIds);
    }

    /**
     * 删除商品三级分类信息
     *
     * @param catId 商品三级分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryByCatId(Long catId) {
        return categoryMapper.deleteCategoryByCatId(catId);
    }

    @Override
    public List<CategoryVo> listWithTree() {
        //1，查出所有分类
        List<Category> entities = categoryMapper.selectCategoryList(new Category());
        // 2. 转换为 CategoryVo 列表
        List<CategoryVo> categoryVos = entities.stream()
            .map(this::convertToVo)
            .collect(Collectors.toList());
        // 3. 构建树结构
        return categoryVos.stream()
            .filter(categoryVo -> categoryVo.getParentCid() == 0)
            .map(menu -> {
                menu.setChildren(getChildrens(menu, categoryVos));
                return menu;
            })
            .sorted((menu1, menu2) -> {
                return Math.toIntExact(
                    (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0
                        : menu2.getSort()));
            })
            .collect(Collectors.toList());
    }

    private CategoryVo convertToVo(Category category) {
        CategoryVo categoryVo = new CategoryVo();
        categoryVo.setCatId(category.getCatId());
        categoryVo.setName(category.getName());
        categoryVo.setParentCid(category.getParentCid());
        categoryVo.setCatLevel(category.getCatLevel());
        categoryVo.setShowStatus(category.getShowStatus());
        categoryVo.setSort(category.getSort());
        categoryVo.setIcon(category.getIcon());
        categoryVo.setProductUnit(category.getProductUnit());
        categoryVo.setProductCount(category.getProductCount());
        return categoryVo;
    }

    private List<CategoryVo> getChildrens(CategoryVo root, List<CategoryVo> all) {
        return all.stream()
            .filter(categoryVo -> categoryVo.getParentCid().equals(root.getCatId()))
            .map(categoryVo -> {
                categoryVo.setChildren(getChildrens(categoryVo, all));
                return categoryVo;
            })
            .sorted((menu1, menu2) -> {
                return Math.toIntExact(
                    (menu1.getSort() == null ? 0 : menu1.getSort()) -
                        (menu2.getSort() == null ? 0 : menu2.getSort())
                );
            })
            .collect(Collectors.toList());
    }
}